package com.guocheng.modules.login.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.guocheng.base.BaseController;
import com.guocheng.entity.AjaxResult;
import com.guocheng.modules.login.model.dto.DeptDTO;
import com.guocheng.modules.login.model.dto.UserDTO;
import com.guocheng.modules.login.model.po.Dept;
import com.guocheng.modules.login.model.po.User;
import com.guocheng.modules.login.service.DeptService;
import com.guocheng.modules.login.service.UserService;
import com.guocheng.modules.login.utils.UserUtils;
import com.guocheng.utils.PasswordUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/sys/user")
@CrossOrigin
public class UserController extends BaseController {

    @Autowired
    private UserService userService;

    @GetMapping("/index")
    public String index() {
        return "sys/user/list";
    }

    /**
     * 查询后台用户列表
     *
     * @param user
     * @return
     */
    @GetMapping("/list")
    @ResponseBody
    public AjaxResult viewUser(UserDTO user) {
        if (user.getMobile() != null && !user.getMobile().equals(""))
            user.setMobile("like_" + user.getMobile());
        if (user.getUsername() != null && !user.getUsername().equals(""))
            user.setUsername("like_" + user.getUsername());
        IPage<User> page = userService.selectPage(user);
        return AjaxResult.ok().data(page.getRecords()).put("total", page.getTotal());
    }

    @GetMapping("/show")
    public String show(@RequestParam(value = "id",required = false)Integer id,Model model){
        //是否为修改判断
        if(id!=null){
            User user1 = userService.selectById(id);
            model.addAttribute("user", user1.setPassword(null));
        }
        return "sys/user/show";
    }

    /**
     * 增加一个后台用户
     *
     * @param user
     * @return
     */
    @PostMapping("/save")
    @ResponseBody
    public AjaxResult createUser(User user) {
        boolean insert = userService.insert(UserUtils.init(user));
        if (insert) {
            return AjaxResult.ok().put("msg", "添加成功");
        }
        return AjaxResult.error().put("msg", "添加失败");
    }

    /**
     * 更改一个后台用户
     *
     * @param user
     * @return
     */
    @PutMapping("/edit")
    @ResponseBody
    public AjaxResult updateUser(User user) {
        Integer id = user.getId();
        if (id == null) {
            return AjaxResult.error().put("msg", "没有id");
        }
        //如果有密码，则加密
        String password = user.getPassword();
        if (password != null && !password.equals("")) {
            password = PasswordUtils.getMD5Password(password);
            user.setPassword(password);
        }else{
            user.setPassword(null);
        }
        //保存
        boolean b = userService.updateById(user);
        if (b) {
            return AjaxResult.ok().put("msg", "修改成功");
        }
        return AjaxResult.error().put("msg", "修改失败");
    }

    /**
     * 删除一个后台用户
     *
     * @param id
     * @return
     */
    @DeleteMapping("/delete")
    @ResponseBody
    public AjaxResult delete(@RequestParam("ids") List<Integer> id) {
        boolean b = userService.deleteByIds(id);
        if(b)return AjaxResult.ok().put("msg", "删除成功");
        return AjaxResult.error().put("msg", "删除失败");
    }

    /**
     * 用户授权角色页面
     *
     * @return
     */
    @GetMapping("/roleView")
    public String userRole() {
        return "sys/user/role";
    }

    /**
     * 获取用户角色信息
     *
     * @param id
     * @return
     */
    @GetMapping("/role")
    @ResponseBody
    public AjaxResult userRoleName(@RequestParam("id") Integer id) {
        List<Integer> integers = userService.selectRoleById(id);
        return AjaxResult.ok().data(integers);
    }

    /**
     * 修改用户角色信息
     * @param id    用户id
     * @param ids   角色ids
     * @return
     */
    @PostMapping("/role")
    @ResponseBody
    public AjaxResult updateuserRoleName(@RequestParam("id") Integer id,
                                         @RequestParam(value = "ids",required = false) List<Integer> ids) {
        boolean b = userService.insertUserRole(id, ids);
        if (b) {
            return AjaxResult.ok("授权成功");
        }
        return AjaxResult.error();
    }
}
